﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class user_uInfofb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (Session.Contents["wj_User"] == null)
            {
                Response.Redirect("../uPassport.aspx");
            }
            else
            {
                if (Request.QueryString["n"] == "edit")　//显示文本信息
                {
                    News_Txt();
                }
                else if (Request.QueryString["n"] == "del")// 删除新闻
                {
                    News_del();
                }
            }
        }
    }
    #region 上传图片
    protected void UpImg_Click(object sender, EventArgs e)//上传图片
    {
        if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
        {

            string fileFullname = this.FileUpload1.FileName;
            string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
            string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
            {
                this.FileUpload1.SaveAs(Server.MapPath("../Images") + "\\" + dataName + "." + type);
                string ProImg = "Images/" + dataName + "." + type;
                this.txtProImg.Text = "Images/" + dataName + "." + type;
            }
            else
            {
                Response.Write("<script language='javascript'>alert('支持格式：|jpg|gif|bmp|');</script>");
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小！');</script>");
        }
    }
    #endregion

    #region 显示文本分类
    private void News_Txt()//显示文本分类
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        DataTable dt = Class1.ExecSel("select * from ZhanHui where n_ID='" + id + "'");
        TextBox1.Text = Convert.ToString(dt.Rows[0]["n_Title"]);
        TextBox2.Text = Convert.ToString(dt.Rows[0]["n_zhtime"]);
        TextBox3.Text = Convert.ToString(dt.Rows[0]["n_zhaddress"]);
        TextBox4.Text = Convert.ToString(dt.Rows[0]["n_Source"]);
        txtProImg.Text = Convert.ToString(dt.Rows[0]["n_Img"]);
        ftbContent.Text = Convert.ToString(dt.Rows[0]["n_Content"].ToString());


        if (dt.Rows[0]["n_Class"].ToString().Trim() == "国内展会")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = true;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "国外展会")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = true;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "展会常识")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = true;
            this.DropDownList1.Items[4].Selected = false;
        }
        else if (dt.Rows[0]["n_Class"].ToString().Trim() == "展馆介绍")
        {
            this.DropDownList1.Items[0].Selected = false;
            this.DropDownList1.Items[1].Selected = false;
            this.DropDownList1.Items[2].Selected = false;
            this.DropDownList1.Items[3].Selected = false;
            this.DropDownList1.Items[4].Selected = true;
        }
    }
    #endregion

    #region 添加新闻
    private void News_Add()　//添加新闻
    {
        string S_Lock="1";
        string Home_Show;
        int n_Hits = 0;
        string Dr = DropDownList1.SelectedItem.Text;
        string Title = TextBox1.Text;
        string Zhtime = TextBox2.Text;
        string Zhaddress = TextBox3.Text;
        string Source = TextBox4.Text;
        string Img = txtProImg.Text;
        string Content = ftbContent.Text.Trim();
        DateTime Time = DateTime.Now;

        if (this.DropDownList1.SelectedItem.Text == "请选择")
        {
            Class1.AlertShow("请选择分类！");
            return;
        }
        else if (Title == "")
        {
            Class1.AlertShow("标题不能为空！");
            return;
        }
        else if (Title.Length > 100)
        {
            Class1.AlertShow("标题不能大于100个字！");
            return;
        }
        string US = Session["wj_User"].ToString();
        DataTable dd = Class1.ExecSel("select * from wj_User where wj_User='" + US + "'");

        string sql = "insert into ZhanHui(n_Class,n_Title,n_zhtime,n_zhaddress,n_Source,n_Img,n_Content,n_Lock,n_Time,n_Hits,n_UserID)values('" + Dr + "','" + Title + "','" + Zhtime + "','" + Zhaddress + "','" + Source + "','" + Img + "','" + Content + "'," + S_Lock + ",'" + Time + "'," + n_Hits + ",'" + dd.Rows[0]["wj_ID"] + "')";
        Class1.ExecSql(sql);
        Class1.AlertShow("发布成功！", "u_Show.aspx?n=add");
    }
    #endregion

    #region 修改新闻
    private void News_Edit()　//修改新闻
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        string S_Lock;
        string Home_Show;
        string Dr = DropDownList1.SelectedItem.Text;
        string Title = TextBox1.Text;
        string Zhtime = TextBox2.Text;
        string Zhaddress = TextBox3.Text;
        string Source = TextBox4.Text;
        string Img = txtProImg.Text;
        string Content = ftbContent.Text.Trim();

        if (this.DropDownList1.SelectedItem.Text == "请选择")
        {
            Class1.AlertShow("请选择分类！");
            return;
        }
        else if (Title == "")
        {
            Class1.AlertShow("标题不能为空！");
            return;
        }
        else if (Title.Length > 100)
        {
            Class1.AlertShow("标题不能大于100个字！");
            return;
        }
        string sql = ("update ZhanHui set n_Class='" + Dr + "',n_Title='" + Title + "',n_zhtime='" + Zhtime + "',n_zhaddress='" + Zhaddress + "',n_Source='" + Source + "',n_Img='" + Img + "',n_Content='" + Content + "' where n_ID='" + id + "'");
        Class1.ExecSql(sql);
        Class1.AlertShow("修改成功！", "uShowManage.aspx");
    }
#endregion

    #region 删除新闻
    private void News_del()//删除新闻
    {
        int id = int.Parse(Request.QueryString["n_ID"]);
        string sql = "delete ZhanHui where n_ID=" + id;
        Class1.ExecSql(sql);
        Class1.AlertShow("删除成功！", "uShowManage.aspx");
    }
    #endregion

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["n"] == "add")
        {
            News_Add();
        }
        else if (Request.QueryString["n"] == "edit")
        {
            News_Edit();
        }
    }
    #region 选择时间
    protected void imgCalendar_Click(object sender, ImageClickEventArgs e)
    {
        calendar.Visible = true;

    }

    protected void calendar_SelectionChanged(object sender, EventArgs e)
    {
        TextBox2.Text = calendar.SelectedDate.ToString("yyyy-MM-dd");
        calendar.Visible = false;

    }


    #endregion
}
